<template>
<div>
    <div class="common-seach-wrap">
      <el-form
        size="small"
        :inline="true"
        :model="searchForm"
        class="demo-form-inline"
      >
        <el-form-item :label="$t('plus.delivery_code')">
          <el-input
            size="small"
            v-model="searchForm.codeNo"
            :placeholder="$t('plus.delivery_code')"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('plus.delivery_status')">
          <el-select
            size="small"
            v-model="searchForm.codeStatus"
            :placeholder="$t('plus.please_select')"
          >
            <el-option :label="$t('plus.all')" value=""></el-option>
            <el-option :label="$t('plus.pending_delivery')" value="0"></el-option>
            <el-option :label="$t('plus.delivered')" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('plus.void_status')">
          <el-select
            size="small"
            v-model="searchForm.isDelete"
            :placeholder="$t('plus.please_select')"
          >
            <el-option :label="$t('plus.all')" value=""></el-option>
            <el-option :label="$t('plus.normal')" value="0"></el-option>
            <el-option :label="$t('plus.voided')" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('plus.coupon_category')">
          <el-select
            size="small"
            v-model="searchForm.categoryId"
            :placeholder="$t('plus.please_select')"
          >
            <el-option :label="$t('plus.all')" value=""></el-option>
            <el-option
              v-for="(item, index) in categoryList"
              :key="index"
              :label="item.name"
              :value="item.categoryId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('plus.related_coupon')">
          <el-select
            size="small"
            v-model="searchForm.cardId"
            filterable
            :placeholder="$t('plus.please_select')"
          >
            <el-option :label="$t('plus.all')" value=""></el-option>
            <el-option
              v-for="(item, index) in cardList"
              :key="index"
              :label="item.cardTitle"
              :value="item.cardId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" icon="Search" @click="onSubmit"
            >{{$t('plus.search')}}</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button
            size="small"
            type="success"
            @click="onExport"
            v-auth="'/order/operate/export'"
            >{{$t('plus.export')}}</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="table-wrap">
      <el-table :data="tableData" style="width: 100%" v-loading="loading">
        <el-table-column prop="codeNo" :label="$t('plus.delivery_code')"></el-table-column>
        <el-table-column prop="codePwd" :label="$t('plus.delivery_password')"></el-table-column>
        <el-table-column prop="cardTitle" :label="$t('plus.related_coupon')"></el-table-column>
        <el-table-column
          prop="startTime"
          :label="$t('plus.delivery_start_date')"
        ></el-table-column>
        <el-table-column prop="endTime" :label="$t('plus.delivery_end_date')"></el-table-column>
        <!--<el-table-column prop="active_status" label="激活状态">
          <template  #default="scope">
            <span v-if="scope.row.active_status == 0" class="gray">未激活</span>
            <span v-if="scope.row.active_status == 1" class="green">已激活</span>
          </template>
        </el-table-column>-->
        <el-table-column prop="codeStatus" :label="$t('plus.delivery_status')">
          <template #default="scope">
            <span v-if="scope.row.codeStatus == 0" class="gray">{{$t('plus.not_delivered')}}</span>
            <span v-if="scope.row.codeStatus == 1" class="green">{{$t('plus.delivered')}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="codeStatus" :label="$t('plus.void_status')">
          <template #default="scope">
            <span v-if="scope.row.isDelete == 1" class="gray">{{$t('plus.voided')}}</span>
            <span v-if="scope.row.isDelete == 0" class="green">{{$t('plus.normal')}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" :label="$t('plus.create_time')"></el-table-column>
        <el-table-column fixed="right" :label="$t('plus.action')" width="50">
          <template #default="scope">
            <el-button
              v-if="scope.row.codeStatus == 0"
              @click="editArticle(scope.row)"
              type="text"
              size="small"
              >{{$t('plus.edit')}}</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!--分页-->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          :current-page="curPage"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="totalDataNumber"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import CardApi from "@/api/card.js";
import { getCookie } from "@/utils/base.js";
import qs from "qs";
import { useUserStore } from "@/store";
const {
  token
} = useUserStore();
export default {
  components: {},
  data() {
    return {
      token,
      /*分类*/
      categoryData: [],
      /*表单数据*/
      tableData: [],
      /*是否打开添加弹窗*/
      open_add: false,
      /*是否打开编辑弹窗*/
      open_edit: false,
      /*当前编辑的对象*/
      userModel: {},
      commentData: [],
      loading: true,
      /*一页多少条*/
      pageSize: 20,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*横向表单数据模型*/
      searchForm: {
        codeNo: "",
        categoryId: "",
        cardId: "",
        codeStatus: "",
        isDelete: ""
      },
      categoryList: [],
      cardList: []
    };
  },
  created() {
    /*获取文章列表*/
    this.getTableList();
  },
  methods: {
    /*获取文章列表*/
    getTableList() {
      let self = this;
      let Params = self.searchForm;
      Params.pageIndex = self.curPage;
      Params.pageSize = self.pageSize;
      CardApi.codelist(Params, true).then(res => {
        self.loading = false;
        self.tableData = res.data.list.records;
        self.totalDataNumber = res.data.list.total;
        self.categoryList = res.data.categoryList;
        self.cardList = res.data.cardList;
      }).catch(error => {
        self.loading = false;
      });
    },
    /*编辑文章*/
    editArticle(row) {
      let params = row.codeId;
      this.$router.push({
        path: "/plus/card/code/edit",
        query: {
          codeId: params
        }
      });
    },
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      self.getTableList();
    },
    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getTableList();
    },
    /*搜索查询*/
    onSubmit() {
      this.curPage = 1;
      this.tableData = [];
      this.getTableList();
    },
    onExport: function () {
      let baseUrl = import.meta.env.VITE_BASIC_URL;
      if (!baseUrl) {
        baseUrl = window.location.protocol + "//" + window.location.host;
      }
      let Params = this.searchForm;
      Params.tokenshop = this.token;
      window.location.href = baseUrl + "/api/shop/plus/card/code/export?" + qs.stringify(Params);
    },
    /*删除文章*/
    deleteCard(row) {
      let self = this;
      ElMessageBox.confirm($t("plus.delete_confirm"), $t("plus.prompt"), {
        confirmButtonText: $t("plus.confirm_text"),
        cancelButtonText: $t("plus.cancel_text"),
        type: "warning"
      }).then(() => {
        self.loading = true;
        CardApi.deleteCard({
          card_id: row.card_id
        }, true).then(data => {
          ElMessage({
            message: data.msg,
            type: "success"
          });
          self.loading = false;
          self.getTableList();
        }).catch(error => {});
      }).catch(() => {});
    },
    handleClick(tab, event) {},
    /*关闭弹窗*/
    closeDialogFunc(e, f) {
      if (f == "add") {
        this.open_add = e.openDialog;
        if (e.type == "success") {
          this.getTableList();
        }
      }
      if (f == "edit") {
        this.open_edit = e.openDialog;
        if (e.type == "success") {
          this.getTableList();
        }
      }
    }
  }
};
</script>

